package com.nanyang.tms.manager.dataimport.impl;

import java.util.Date;
import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.domain.DataImportDomain;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.sys.api.dto.request.obsauth.ObsAuthGetFilePathRequest;
import com.nanyang.sys.api.dto.response.obsauth.ObsAuthGetFilePathResponse;
import com.nanyang.sys.api.service.obsauth.ObsAuthRpcService;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.dict.TmsCommonEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.domain.dataimport.DataImportDO;
import com.nanyang.tms.domain.dataimport.DataImportLineQuery;
import com.nanyang.tms.domain.dataimport.DataImportLineVO;
import com.nanyang.tms.domain.dataimport.DataImportQuery;
import com.nanyang.tms.domain.dataimport.DataImportVO;
import com.nanyang.tms.manager.dataimport.DataImportManager;
import com.nanyang.tms.mongo.DataImportDao;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component("dataImportManager")
public class DataImportManagerImpl implements DataImportManager{
    @Autowired
	private DataImportDao dataImportDao;
	@Autowired
    private GlobalIdGeneratorUtil globalIdGeneratorUtil;
	@Reference(version = "1.0.0", timeout = 10000, check = false)
	private ObsAuthRpcService obsAuthRpcService;
	@Autowired
	private RocketMQTemplate rocketMQTemplate;
	
	@Override
	public void saveImportFile(DataImportDO dataImportDO) {
		
		try {
			ObsAuthGetFilePathRequest req=new ObsAuthGetFilePathRequest();
			req.setSignUrl(dataImportDO.getFileUrl());
			ApiResult<ObsAuthGetFilePathResponse> filePathBySignUrl = obsAuthRpcService.getFilePathBySignUrl(req);
			if(CommonEnum.REQUEST_SUCCESS.getCode() == filePathBySignUrl.getCode()){
				dataImportDO.setFileUrl(filePathBySignUrl.getData().getFilePath());
				dataImportDO.setFileCode(TmsSerializeConstant.FILE_IMPORT_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.FILE_IMPORT_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY));
				dataImportDO.setGmtCreate(new Date());
				dataImportDO.setStatus(0);
				dataImportDO.setResult("处理中");
				DataImportDO insertDataImport = dataImportDao.insertDataImport(dataImportDO);
				rocketMQTemplate.asyncSendOrderly(dataImportDO.getServiceType(), MessageBuilder.withPayload(insertDataImport).setHeader(RocketMQHeaders.KEYS, insertDataImport.getFileCode()).build(), insertDataImport.getCompanyCode(), new SendCallback() {
					@Override
					public void onSuccess(SendResult sendResult) {
						log.info(sendResult.toString());
					}
					
					@Override
					public void onException(Throwable e) {
						
					}
				});
			}else{
				throw new TmsManagerException(filePathBySignUrl.getCode(),filePathBySignUrl.getMessage());
			}
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		
		
	}

	@Override
	public PageBean<DataImportVO> listDataImport(DataImportQuery dataImportQuery) {
		if(dataImportQuery==null){
			throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode(),TmsCommonEnum.PARAM_IS_NULL.getMessage());
		}
		try {
			long count=dataImportDao.countDataImport(dataImportQuery);
			PageTableBean<DataImportVO> page = new PageTableBean<DataImportVO>(dataImportQuery.getPageIndex(), dataImportQuery.getPageSize());
			if(count>0){
				page.setTotalItem(new Long(count).intValue());
	       	    int startRow = page.getStartRow();
	       	    if (startRow > 0) {
						 startRow -= 1;
				}
	       	 dataImportQuery.setStartRow(startRow);
	       	List<DataImportVO> listDataImport = dataImportDao.listDataImport(dataImportQuery) ;
	       	page.addAll(listDataImport);
			}
			return page;
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		
	}

	@Override
	public PageBean<DataImportLineVO> listDataImportLine(DataImportLineQuery dataImportLineQuery) {
		if(dataImportLineQuery==null){
			throw new TmsManagerException(TmsCommonEnum.PARAM_IS_NULL.getCode(),TmsCommonEnum.PARAM_IS_NULL.getMessage());
		}
		try {
			long count=dataImportDao.countDataImportLine(dataImportLineQuery);
			PageTableBean<DataImportLineVO> page = new PageTableBean<DataImportLineVO>(dataImportLineQuery.getPageIndex(), dataImportLineQuery.getPageSize());
			if(count>0){
				page.setTotalItem(new Long(count).intValue());
	       	    int startRow = page.getStartRow();
	       	    if (startRow > 0) {
						 startRow -= 1;
				}
	       	 dataImportLineQuery.setStartRow(startRow);
	       	List<DataImportLineVO> listDataImport = dataImportDao.listDataImportLine(dataImportLineQuery) ;
	       	page.addAll(listDataImport);
			}
			return page;
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
	}

}
